package demo.sicau.votesystem.controller;

import demo.sicau.votesystem.entity.PO.UserPO;
import demo.sicau.votesystem.entity.VO.ResultVO;
import demo.sicau.votesystem.service.AdminService;
import demo.sicau.votesystem.service.UserService;
import demo.sicau.votesystem.util.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @GetMapping("/getAllUser")
    public ResultVO getAllUser(){
        return userService.getAllUser();
    }

    @GetMapping("getUser")
    public ResultVO getUser(@RequestParam("page") int page,//当前页面
                            @RequestParam("limit") int limit){ //limit——每个页面的数量多少
        return userService.getUser(page,limit);
    }

    @PostMapping("/login")
    public ResultVO login(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("identity") String identity,
                          @RequestParam("role") int role,
                           HttpServletRequest request){

        if(Integer.parseInt(identity)==0)
            return userService.login(role,username,password,request);
        else
            return adminService.adminLogin(username,password,request);

    }


    @PostMapping("/deleteUserById")
    public ResultVO deleteUserById(@RequestParam("uid") String uid){
        return userService.deleteUserById(uid);
    }

    @PostMapping("/addUser")
    public ResultVO addUser(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            @RequestParam("realName") String realName,
                            @RequestParam(name = "email",required = false) String email){
        UserPO userPO = new UserPO(IDUtil.getUUID(),username,password,realName,email);
        return userService.addUser(userPO);
    }

    @GetMapping("/updateUser")
    public ResultVO updateUser(@RequestParam("uid") String uid,
                               @RequestParam("password") String password,
                               @RequestParam("realName") String realName,
                               @RequestParam(name = "email",required = false) String email){
        UserPO userPO = new UserPO(uid,password,realName,email);
        return userService.updateUser(userPO);
    }



}
